<?php
Class Controller_Manage_Sys extends Controller_Abstract
{
	//增加图片企业信息，增加基本信息
	function actionCompanyList()
	{
		$this->_view['title'] = '公司信息管理';
		//滚动图列表和基本信息设置都会显示在同一个页面，提示最多上传10张图
		$com = Setting::find()->asArray()->getOne();
		$this->_view['com'] = $com;
	}
	
	function actionCompanyListSave()
	{
		$this->_view['title'] = '公司信息管理保存';
		if ($_POST)
		{
			if(isset($_FILES['file-img']['size'])&&$_FILES['file-img']['size']>0)
			{
				$img = Helper_BSS_Normal::upload($_FILES['file-img'], Q::ini('custom_system/base_url').'upload/logo/img');//大图
				if ($img['ack'] == SUCCESS)
				{
					$imgsrc = $img['data'];
				}
				else
				{
					return $this->_redirectMessage('提交失败', "LOGO上传失败，请重新选择图片", $_SERVER['HTTP_REFERER'],FAILURE);
				}
			}
			
			$info = Setting::find('id=?', $_POST['id'])->getOne();
			if ($info['id'])
			{
				$info->company_name = isset($_POST['name']) ? trim($_POST['name']) : '';
				$info->company_url = isset($_POST['url']) ? trim($_POST['url']) : '';
				$info->company_contact = isset($_POST['contact']) ? trim($_POST['contact']) : '';
				$info->content = isset($_POST['content']) ? trim($_POST['content']) : '';
				if(isset($imgsrc)&&$imgsrc)
				{
					$info->img = $imgsrc;
				}
				$info->create_time = CURRENT_DATETIME;
				$info->save();
				return $this->_redirectMessage('修改成功', '正在返回', $_SERVER['HTTP_REFERER'], SUCCESS);
			}
			else
			{
				$return = Setting::create($_POST);
				if ($return['ack'] == SUCCESS)
				{
					return $this->_redirectMessage('添加成功', '正在返回', $_SERVER['HTTP_REFERER'], SUCCESS);
				}
				else
				{
					return $this->_redirectMessage('添加失败', '正在返回', $_SERVER['HTTP_REFERER'], FAILURE);
				}
			}
		}
		else
		{
			return $this->_redirectMessage('添加或修改失败', '正在返回', $_SERVER['HTTP_REFERER'], FAILURE);
		}
	}
	
	function actionSetting()
	{
		$this->_view['title'] = '系统设置';
		$com = Setting::find()->asArray()->getOne();
		$this->_view['com'] = $com;
	}
	
	function actionSettingSave()
	{
		$this->_view['title'] = '系统设置保存';
		if ($_POST)
		{
			$info = Setting::find('id=?', $_POST['id'])->getOne();
			if ($info['id'])
			{
				$info->sitename = isset($_POST['sitename']) ? trim($_POST['sitename']) : '';
				$info->keyword = isset($_POST['keyword']) ? trim($_POST['keyword']) : '';
				$info->description = isset($_POST['description']) ? trim($_POST['description']) : '';
				$info->copyright = isset($_POST['copyright']) ? trim($_POST['copyright']) : '';
				$info->bean = isset($_POST['bean']) ? trim($_POST['bean']) : '';
				$info->sumcode = isset($_POST['sumcode']) ? trim($_POST['sumcode']) : '';
				$info->create_time = CURRENT_DATETIME;
				$info->save();
				return $this->_redirectMessage('修改成功', '正在返回', $_SERVER['HTTP_REFERER'], SUCCESS);
			}
			else
			{
				$return = Setting::create($_POST);
				if ($return['ack'] == SUCCESS)
				{
					return $this->_redirectMessage('添加成功', '正在返回', $_SERVER['HTTP_REFERER'], SUCCESS);
				}
				else
				{
					return $this->_redirectMessage('添加失败', '正在返回', $_SERVER['HTTP_REFERER'], FAILURE);
				}
			}
		}
	}
	
	function actionLog()
	{
		$this->_view['title'] = '日志（登录）';
	}
	
	function actionCreate()
	{
		$this->_view['title'] = '企业简介图';
	}
	
	function actionCreateSave()
	{
		$this->_view['title'] = '企业简介图保存';
		//dump($_FILES);exit;
		$notes = isset($_POST['notes'])?$_POST['notes']:'';
		$img = isset($_POST['img'])?$_POST['img']:'';
		$time = date('Y-m-d H:i:s');
		if($_FILES['file-img']['size']>0)
		{
			$img = Helper_BSS_Normal::upload($_FILES['file-img'], Q::ini('custom_system/base_url').'upload/sys/img');//大图
			if ($img['ack'] == SUCCESS)
			{
				$imgsrc = $img['data'];
			}
			else
			{
				return $this->_redirectMessage('提交失败', "图片上传失败，请重新选择图片", $_SERVER['HTTP_REFERER'],FAILURE);
			}
		}
		
		if($img)
		{	
			$new = new VegePfImg();
			$new->img = $imgsrc;
			$new->notes = $notes;
			$new->save();//终极方法
			return $this->_redirectMessage('提交成功', "正在返回..", url('Manage::Sys/Create'),SUCCESS);
		}
		else
		{
			return $this->_redirectMessage('提交失败', "请选择图片", url('Manage::Sys/Create'),FAILURE);
		}
	}
	
	function actionList()
	{
		$this->_view['title'] = '企业见截图列表';
		$cp = isset($_GET['cp']) ? $_GET['cp'] : 1;
		$ps = isset($_GET['ps']) ? $_GET['ps'] : 20;
		$url_arr = Helper_BSS_Normal::buildCondition();
		$result = VegePfImg::search($cp, $ps, $url_arr);
		if ($result['ack'] == SUCCESS)
		{
			$page = new Helper_BSS_Pagec($result['pagination'], url('Manage::Sys/List', $url_arr));
			$this->_view['page'] = $page->getPage();
            $this->_view['pagecount'] = $result['pagination'];
			$this->_view['list'] = $result['data'];
		}
	}
	
	function actionEdit()
	{
		$this->_view['title'] = '企业图片编辑';
		$id = isset($_GET['id']) ? $_GET['id'] : 0;
		$vegepfimg = VegePfImg::find('id=?', $id)->asArray()->getOne();
		if ($vegepfimg)
		{
			$this->_view['vegepfimg'] = $vegepfimg;
		}
		else
		{
			return $this->_redirectMessage('编辑失败', '图片不存在', url('Manage::Sys/List'), FAILURE);
		}
	}
	
	function actionEditSave()
	{
		$this->_view['title'] = '企业图片编辑保存';
		if (isset($_POST)&&$_POST)
		{
			$id = isset($_POST['id'])?$_POST['id']:'';
			$img = isset($_POST['img'])?$_POST['img']:'';
			$notes = isset($_POST['notes'])?$_POST['notes']:'';
			if(isset($_FILES['file-img']['size'])&&$_FILES['file-img']['size']>0)
			{
				$img = Helper_BSS_Normal::upload($_FILES['file-img'], Q::ini('custom_system/base_url').'upload/sys/img');//大图
				if ($img['ack'] == SUCCESS)
				{
					$imgsrc = $img['data'];
				}
				else
				{
					return $this->_redirectMessage('提交失败', "大图上传失败，请重新选择图片", $_SERVER['HTTP_REFERER'],FAILURE);
				}
			}
			
			if($id)
			{
				$edit = VegePfImg::find('id=?', $id)->getOne();
				if(isset($imgsrc)&&$imgsrc)
				{
					$edit->img = $imgsrc;
				}
				$edit->notes = $notes;
				$edit->save();//终极方法
				
				return $this->_redirectMessage('提交成功', "正在返回..", $_SERVER['HTTP_REFERER'],SUCCESS);//只返回上一页即可，这样好定位
			}
			else
			{
				return $this->_redirectMessage('提交失败', "信息不存在", $_SERVER['HTTP_REFERER'],FAILURE);
			}
		}
		else
		{
			return $this->_redirectMessage('编辑失败', '请检查提交的数据是否正确', url('Manage::Sys/List'), FAILURE);
		}
	}
	
	function actionDelete()
	{
		$this->_view['title'] = '删除';
		$result = VegePfImg::meta()->deleteWhere(array('id'=>$_GET['id']));
		if (!$result)
		{
            echo 1;exit;
		}
		else
		{
            echo 0;exit;
		}
	}
	
	function actionDeleteAll()
	{
		$this->_view['title'] = '勾选删除';
		if ($this->_context->isGET())
		{
			$ids = explode(',', $_GET['ids']);
			$msg = '';
			foreach ($ids as $i)
			{
				$result = VegePfImg::meta()->deleteWhere(array('id'=>$i));
				if (!$result)
				{
					$msg = '';
				}
				else
				{
					$msg = $i.'删除失败';
				}
			}

			if (!$msg)
			{
				return $this->_redirectMessage('批量删除成功', '正在返回..', $_SERVER['HTTP_REFERER'], SUCCESS);
			}
			else
			{
				return $this->_redirectMessage('删除失败', '请检查提交的数据是否存在', $_SERVER['HTTP_REFERER'], FAILURE);
			}
		}
	}
	
	function actionExportBusinessBaseInfo()
	{
		$this->_view['title'] = '导出供应商信息';
		set_time_limit(0);
		$id = isset($_GET['id']) ? $_GET['id'] : 0;
		$vegesupplier = VegeSupplier::find()->asArray()->getAll();
		//dump($businessbaseinfo);exit;
		if ($vegesupplier)
		{
			$baseinfo = BaseNodeInfo::find()->asArray()->getOne();
			
			$filename = $baseinfo['companynumber'] . '_KHDA.txt'; //设置文件名
			header('Content-Type: text/txt');
			header("Content-Disposition: attachment;filename={$filename}");
			$out = fopen('php://output', 'w');
			foreach($vegesupplier as $vs)
			{
				$line .= ",".$vs['suppliernumber'];
				$line .= ",".$vs['suppliername'];
				$line .= ",".$vs['regnum'];
				$line .= ",".str_replace('-','',$vs['recorddate']);
				$line .= ",".$vs['corporate'];
				$line .= ",".$vs['phone'];
				$line .= ",".str_replace('-','',$vs['updatetime']);
				fputs($out, $line."\r\n");
			}
			
			fclose($out);
		}
		else
		{
			return $this->_redirectMessage('导出失败', '没有任何信息', url('Manage::Sys/List'), FAILURE);
		}
	}
	
	function actionLogList()
	{
		$this->_view['title'] = '日志列表';
		$cp = isset($_GET['cp']) ? $_GET['cp'] : 1;
		$ps = isset($_GET['ps']) ? $_GET['ps'] : 20;
		$url_arr = Helper_BSS_Normal::buildCondition();
		$result = VegeLog::search($cp, $ps, $url_arr);
		if ($result['ack'] == SUCCESS)
		{
			$page = new Helper_BSS_Pagec($result['pagination'], url('Manage::Sys/LogList', $url_arr));
			$this->_view['page'] = $page->getPage();
            $this->_view['pagecount'] = $result['pagination'];
			$this->_view['list'] = $result['data'];
		}
	}
	
	function actionLogDelete()
	{
		$this->_view['title'] = '删除';
		$result = VegeLog::meta()->deleteWhere(array('id'=>$_GET['id']));
		if (!$result)
		{
            echo 1;exit;
		}
		else
		{
            echo 0;exit;
		}
	}
	
	function actionLogDeleteAll()
	{
		$this->_view['title'] = '勾选删除';
		if ($this->_context->isGET())
		{
			$ids = explode(',', $_GET['ids']);
			$msg = '';
			foreach ($ids as $i)
			{
				$result = VegeLog::meta()->deleteWhere(array('id'=>$i));
				if (!$result)
				{
					$msg = '';
				}
				else
				{
					$msg = $i.'删除失败';
				}
			}

			if (!$msg)
			{
				return $this->_redirectMessage('批量删除成功', '正在返回..', $_SERVER['HTTP_REFERER'], SUCCESS);
			}
			else
			{
				return $this->_redirectMessage('删除失败', '请检查提交的数据是否存在', $_SERVER['HTTP_REFERER'], FAILURE);
			}
		}
	}
}